package file_collect

//收藏
import (
	"github.com/goccy/go-json"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/os/glog"
	"gitlab.local/DO-module/new-filemanage-module/src/constant"
	"gitlab.local/TerraMaster/tos-modules/tosconfig"
	"gitlab.local/golibrary/utils"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"
)

type Collect struct {
}

// CollectConf 收藏文件配置
type CollectConf struct {
	Path       string    `json:"path"`        //文件路径
	CreateTime time.Time `json:"create_time"` //创建时间
}

func New() *Collect {
	return &Collect{}
}

// IsCollect 判断用户是否有收藏文件
func (c *Collect) IsCollect(uname string) bool {
	conf, e := c.GetAllCollectConf(uname)
	if e != nil {
		glog.Warning(e, "IsCollect")
		return false
	}
	return len(conf) > 0
}

// 校验收藏文件
func (c *Collect) verifyCollectFile(data map[string]CollectConf) map[string]CollectConf {
	for k, v := range data {
		if !utils.Exists(v.Path) {
			delete(data, k)
		}
	}
	return data
}

// IsCollectList 判断是否收藏
func (c *Collect) IsCollectList(uname, p string) bool {
	conf, err := c.GetAllCollectConf(uname)
	if err != nil {
		return false
	}
	for _, v := range conf {
		if strings.TrimRight(v.Path, "/") == strings.TrimRight(p, "/") {
			return true
		}
	}
	return false
}

// AddCollectFilter 添加收藏(过滤转换路径)
func (c *Collect) AddCollectFilter(uname string, data []string) error {
	cf := make([]CollectConf, 0)
	for _, v := range data {
		cf = append(cf, CollectConf{Path: v})
	}
	return c.AddCollect(uname, cf)
}

// AddCollect 添加收藏
func (c *Collect) AddCollect(uname string, data []CollectConf) error {
	conf, e := c.GetAllCollectConf(uname)
	if e != nil {
		return gerror.Wrap(e, "AddCollect")
	}
	for _, v := range data {
		if !utils.Exists(v.Path) {
			continue
		}
		conf[url.QueryEscape(v.Path)] = CollectConf{
			Path:       v.Path,
			CreateTime: time.Now(),
		}
	}
	if e = c.saveAllCollectConf(uname, conf); e != nil {
		return gerror.Wrap(e, "AddCollect")
	}
	return nil
}

// DelCollect 删除收藏
func (c *Collect) DelCollect(uname string, paths []string) error {
	if len(paths) == 1 {
		if e := c.delCollectConf(uname, paths[0]); e != nil {
			return gerror.Wrap(e, "DelCollect")
		}
		return nil
	}
	for _, v := range paths {
		if e := c.delCollectConf(uname, v); e != nil {
			return gerror.Wrap(e, "DelCollect")
		}
	}
	return nil
}

// delCollectConf 删除收藏
func (c *Collect) delCollectConf(uname string, path string) error {
	conf, e := c.GetAllCollectConf(uname)
	if e != nil {
		return gerror.Wrap(e, "delCollectConf")
	}
	delete(conf, url.QueryEscape(path))
	return c.saveAllCollectConf(uname, conf)
}

// saveAllCollectConf 保存配置
func (c *Collect) saveAllCollectConf(uname string, cf map[string]CollectConf) error {
	marshal, err := json.Marshal(cf)
	if err != nil {
		return gerror.Wrap(err, "saveAllCollectConf")
	}
	src, err := c.collectConfDir(uname)
	if err != nil {
		return gerror.Wrap(err, "saveAllCollectConf1")
	}
	return os.WriteFile(src, marshal, os.ModePerm)
}

// GetAllCollectConf 获取所有收藏配置
func (c *Collect) GetAllCollectConf(uname string) (map[string]CollectConf, error) {
	cfs := make(map[string]CollectConf)
	src, e := c.collectConfDir(uname)
	if e != nil {
		return cfs, gerror.Wrap(e, "getAllCollectConf1")
	}
	if !utils.Exists(src) {
		return cfs, nil
	}
	content, e := os.ReadFile(src)
	if e != nil {
		return cfs, gerror.Wrap(e, "getAllCollectConf2")
	}
	if len(content) > 0 {
		if e = json.Unmarshal(content, &cfs); e != nil {
			return cfs, gerror.Wrap(e, "getAllCollectConf3")
		}
	}
	//检测收藏数据
	data := c.verifyCollectFile(cfs)
	if len(cfs) != len(data) {
		_ = c.saveAllCollectConf(uname, data)
	}
	return data, nil
}

// collectConfDir 收藏配置文件目录
func (c *Collect) collectConfDir(uname string) (string, error) {
	cfg, err := tosconfig.GetUserConfigDir(uname)
	if err != nil {
		return "", gerror.Wrap(err, "collectConfDir")
	}
	return filepath.Join(cfg, constant.CollectConf), nil
}
